# -*- coding: utf-8 -*-
# @Time    : 2020/6/17 下午10:53
# @Author  : caotian
# @FileName: eval.py
# @Software: PyCharm
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D,Pool2D,Linear
import json
import gzip
import os
import sys
import random
import numpy as np
from PIL import Image
curpath=os.path.abspath(os.curdir)
sys.path.append(curpath)
import optimizationdata as od
import optimizationmodel as om
with fluid.dygraph.guard():
    print("evaluation start:")
    model=om.MNIST()
    model_state_dict,_=fluid.load_dygraph('mnist-model')
    model.load_dict(model_state_dict)
    model.eval()
    eval_loader=od.load_data('eval')
    acc_set=[]
    avg_loss_set=[]
    for batch_id,data in enumerate(eval_loader()):
        x_data,y_data=data
        img=fluid.dygraph.to_variable(x_data)
        label=fluid.dygraph.to_variable(y_data)
        prediction,acc=model(img,label)
        loss=fluid.layers.cross_entropy(input=prediction,label=label)
        avg_loss=fluid.layers.mean(loss)
        acc_set.append(float(acc.numpy()))
        avg_loss_set.append(float(avg_loss.numpy()))
    acc_val_mean=np.array(acc_set).mean()
    avg_loss_val_mean=np.array(avg_loss_set).mean()
    print('loss={},acc={}'.format(avg_loss_val_mean,acc_val_mean))